﻿@inject IDataCacheService _dataCacheService
@inject IServiceProvider _serviceProvider
@inject IHttpService _httpService

@implements IDisposable

<ErrorHandler IsSSR="ToolHelper.IsSSR" ShowDetailedErrorInfor="_dataCacheService.ThemeSetting.ShowDetailedErrorInfor">
    <KanbanCard @ref="KanbanCard" />
    <CnGalWebSite.Components.Images.ImagesLargeViewDialog />
    <CnGalWebSite.Shared.MasaComponent.Shared.Tips.UserLicenseAgreementTip />
    <CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent @ref="trackEven" />
    <CnGalWebSite.Shared.MasaComponent.Shared.Tips.BackgroundImageTip/>
    <audio id="bgMusic"></audio>
    <CascadingValue Value=this IsFixed="true">
        @ChildContent
    </CascadingValue>

</ErrorHandler>



@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    public KanbanCard KanbanCard { get; set; }


    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent trackEven;

    private System.Threading.Timer mytimer;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {

            //启动定时器
            mytimer = new System.Threading.Timer(new System.Threading.TimerCallback(Send), null, 0, 1000 * 60 * 10);
            var user = await authenticationStateTask;
            if (user.User.Identity.IsAuthenticated)
            {
                if (trackEven != null)
                {
                    var (email, name) = user.User.Claims.GetExternalUserInfor();
                    await trackEven.SendTrackEvent(TrackEventType.Login, TrackEventDataType.User, user.User.Claims.GetUserId(), name);
                }
            }
        }
    }
    public async void Send(object o)
    {
        await InvokeAsync(async () =>
        {
            try
            {
                var user = await authenticationStateTask;
                if (user.User.Identity.IsAuthenticated)
                {
                    await _httpService.GetAsync<Result>("api/account/MakeUserOnline");
                }
            }
            catch
            {

            }
        });

    }
    #region 释放实例
    public void Dispose()
    {
        if (mytimer != null)
        {
            mytimer?.Dispose();
        }
        GC.SuppressFinalize(this);
    }
    #endregion

}
